package com.bloveoacom.weichi.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bloveoacom.base.BaseList2Activity;
import com.bloveoacom.http.HttpMethods;
import com.bloveoacom.http.RxHelper;
import com.bloveoacom.observer.BaseSubscriber2;
import com.bloveoacom.util.AppUtil;
import com.bloveoacom.util.DisplayUtil;
import com.bloveoacom.util.GlideCacheUtil;
import com.bloveoacom.weichi.R;
import com.bloveoacom.weichi.bean.FirBean;
import com.bloveoacom.weichi.bean.ItemOptionBean;
import com.bloveoacom.weight.DrawableCenterTextView;
import com.bloveoacom.weight.LlkjToolBar;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;

import static com.bloveoacom.util.DialogUtil.showExitDialog;

/**
 * author: zhu.px
 * e-mail: 1271901673@qq.com
 * time  : 2018/7/12
 * desc  : 设置
 */
public class SettingActivity extends BaseList2Activity<ItemOptionBean> {

    @BindView(R.id.title_bar)
    LlkjToolBar titleBar;

    @Override
    public int getLayoutResId() {
        return R.layout.layout_refresh_title;
    }


    @Override
    public void addViewForAdapter() {
        adapter.addFooterView(getFooterView());
    }
    String cacheSize = "0KB";
    @Override
    public void initView() {
        super.initView();
        titleBar.setToolBar("设置",
                true, "", R.drawable.ic_back, false, "", LlkjToolBar.isNull);
        cacheSize = GlideCacheUtil.getInstance().getCacheSize(this);
    }

    @Override
    protected View getViewSetTB() {
        return titleBar;
    }

    @Override
    public void initData() {
        super.initData();
        HttpMethods.getInstance().getHttpService().latest().compose(RxHelper.io_main()).subscribeWith(new BaseSubscriber2<FirBean>(this) {
            @Override
            public void onNext(FirBean o) {
                super.onNext(o);
                bean =  o;
                isHasNews = bean.version> AppUtil.getCurrentVersion(mContext).versionCode;
                datas.get(1).endData = isHasNews ? "有新版本" : "已是最新";
                adapter.notifyDataSetChanged();
            }
        });
    }

    ViewHolder holder;
    public View getFooterView() {
        View footer = LayoutInflater.from(mContext).inflate(R.layout.footer_setting, null);
         holder = new ViewHolder(footer);
        return footer;
    }


    @Override
    public void getBundleExtras(Bundle extras) {

    }

    @Override
    protected boolean isOpenRefresh() {
        return false;
    }

    @Override
    public boolean isLoadMore() {
        return false;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_option_data;
    }

    @Override
    protected void convertView(BaseViewHolder helper, ItemOptionBean item) {
        TextView textView = helper.getView(R.id.tv_item_start);
        TextView endtv = helper.getView(R.id.tv_item_end);
        DisplayUtil.setDrawbleLeft(mContext, textView, item.startPic, 10);
        if (item.endPic == 0) {
            DisplayUtil.setDrawbleClear(endtv);
        }
        textView.setText(item.startData);
        endtv.setText(item.endData);
    }

    @Override
    protected void getData() {
        List<ItemOptionBean> ops = getOps();
        ops.get(0).endData = cacheSize;
        doData(ops);
    }

    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ItemOptionBean itemOptionBean = datas.get(position);
                switch (itemOptionBean.startPic) {
                    case R.drawable.ic_clear_cache:
                        showLoadingDialog("清除中...");
                        GlideCacheUtil.getInstance().clearImageDiskCache(mContext);
                        Observable.timer(2, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
                            @Override
                            public void accept(Long aLong) throws Exception {
                                datas.get(0).endData = "0KB";
                                adapter.notifyDataSetChanged();
                                 hideLoadingDialog();
                            }
                        });
                        break;
                    case R.drawable.ic_version_update:
                        if (!isHasNews) {
                            return;
                        }
                        new MaterialDialog.Builder(SettingActivity.this)
                                .title("有新版本" + bean.versionShort)
                                .content("版本更新" + AppUtil.getCurrentVersion(mContext).versionName)
                                .positiveText("安装")
                                .negativeText("取消")
                                .onPositive((dialog, which) -> {
                                    try {
                                        Intent intent = new Intent();
                                        intent.setAction("android.intent.action.VIEW");
                                        Uri content_url = Uri.parse(bean.installUrl);
                                        intent.setData(content_url);
                                        intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
                                        startActivity(intent);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                        Uri uri = Uri.parse(bean.installUrl);
                                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                        startActivity(intent);
                                    }
                                })
                                .show();
                        break;
                }
            }
        });

    }

    //数据
    public static List<ItemOptionBean> getOps() {
        List<ItemOptionBean> ops = new ArrayList<>();
        ItemOptionBean itemOptionBean = new ItemOptionBean(R.drawable.ic_clear_cache, 0, -1, "清楚缓存", "");
        ops.add(itemOptionBean);
        itemOptionBean = new ItemOptionBean(R.drawable.ic_version_update, 0, -1, "检查更新", "已是最新版本");
        ops.add(itemOptionBean);
        return ops;
    }

    FirBean bean;
    boolean isHasNews = false;


     class ViewHolder {
        @BindView(R.id.btn_exit)
        DrawableCenterTextView btnExit;
         @OnClick(R.id.btn_exit)
         public void click(){
             showExitDialog();
         }
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
